#include "common.h"

union flash_adaptor_parm_all flash_adaptor_parm;

void write_flash_adaptor_parm(void)
{
    flash_unlock();
    flash_clear_flag();
    flash_erase_page(ADA_PARM_ADDR);
    flash_write_halfword(ADA_PARM_ADDR, (void *)&flash_adaptor_parm, sizeof(flash_adaptor_parm) / 2);
    flash_lock();
}

void get_adaptor_parm(void)
{
    volatile unsigned short *p    = (void *)&flash_adaptor_parm;
    volatile unsigned short *addr = (void *)ADA_PARM_ADDR;
    for (unsigned int i = 0; i < sizeof(flash_adaptor_parm) / 2; i++) {
        *p = *addr;
        p++;
        addr++;
    }

    if (0xA5 != flash_adaptor_parm.part.bcp) {
        adaptor_parm.can_id              = 34;
        flash_adaptor_parm.part.ada_parm = adaptor_parm;
        flash_adaptor_parm.part.bcp      = 0xa5;
        write_flash_adaptor_parm();
    } else {
        adaptor_parm = flash_adaptor_parm.part.ada_parm;
    }
}
